package com.enniswu.weitter.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;

import com.enniswu.weitter.R;

public class PopupPicsSelector extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {

    private Activity activity;
    private OnPickListener listener;

    public PopupPicsSelector(Activity activity, OnPickListener listener) {
        super(activity);
        this.activity = activity;
        this.listener = listener;
        View view = View.inflate(activity, R.layout.pop_photo,null);
        setContentView(view);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setFocusable(true);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.PopupWindow_photo_anim);

        setOnDismissListener(this);
        view.findViewById(R.id.bt_photos).setOnClickListener(this);
        view.findViewById(R.id.bt_camera).setOnClickListener(this);
        view.findViewById(R.id.bt_cancel).setOnClickListener(this);
    }

    @Override
    public void onDismiss() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1.0f;
        activity.getWindow().setAttributes(lp);
    }

    @Override
    protected void finalize() throws Throwable {
        activity = null;
        super.finalize();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_photos:
                listener.fromGallery();
                break;

            case R.id.bt_camera:
                listener.fromCapture();
                break;

            default:
                break;
        }
        dismiss();
    }

    public void show(View view) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.5f;
        activity.getWindow().setAttributes(lp);
        this.showAtLocation(view, Gravity.BOTTOM,0,50);
    }

    public interface OnPickListener {
        void fromGallery();
        void fromCapture();
    }
}
